//
//  THJFabuManageVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/30.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJFabuManageVC.h"
#import "THJFabuManageCell.h"
#import "THJFabuSegmentCell.h"

@interface THJFabuManageVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableview;
    NSInteger clickType;
    NSInteger  clickIndex;
}
@property(nonatomic,strong)NSArray * xsArr;
@property(nonatomic,strong)NSArray * xhArr;
@property(nonatomic,strong)NSArray * gyArr;
@property(nonatomic,strong)NSArray * qgArr;
@end

@implementation THJFabuManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setuptableview];
    self.title = @"发布管理";
    [self getDataWithType:0];
    clickType = 0;
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)getDataWithType:(NSInteger)type{
    NSString * mobile = [THJTools getMobile];
    NSString * token = [THJTools getToken];
    //NSString * token = @"dev";
    NSString * urlStr = [NSString stringWithFormat:@"%@%@%@%@%@%ld%@%@",THJDomain,THJStorePHP,@"action=infodata&mobile=",mobile,@"&type=",type,@"&token=",token];
    __weak typeof(self)  weakself = self;
    [THJNetWork getCompanyIntroFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view];
        weakself.xhArr = receiveData[@"data"];
        [_tableview reloadData];
    }Type:2];
}
-(void)setuptableview{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight+44) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJFabuManageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableview registerNib:[UINib nibWithNibName:@"THJFabuSegmentCell" bundle:nil] forCellReuseIdentifier:@"seg"];
    _tableview.delegate = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.dataSource =  self;
    [self.view addSubview:_tableview];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 40;
    }else{
        return 194;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.xhArr.count+1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if(indexPath.row == 0){
        THJFabuSegmentCell * cell = [tableView dequeueReusableCellWithIdentifier:@"seg" forIndexPath:indexPath];
        [cell.segmentCtrl addTarget:self action:@selector(segmentCtrlValueChanged:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }else{
        THJFabuManageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.updateBtn.hidden = YES;
        cell.editBtn.hidden = YES;
        [cell.updateBtn addTarget:self action:@selector(updateBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.editBtn addTarget:self action:@selector(editBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.moreBtn addTarget:self action:@selector(moreBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        cell.updateBtn.tag = indexPath.row;
        cell.editBtn.tag = indexPath.row+100;
        cell.moreBtn.tag = indexPath.row + 1000;
        NSDictionary * dic = self.xhArr[indexPath.row-1];
        [cell updateCellContentWithDic:dic Type:clickType];
        return cell;
    }
}
-(void)segmentCtrlValueChanged:(UISegmentedControl*)sgmtCtrl{
    NSLog(@"点击了segment");
    clickType = sgmtCtrl.selectedSegmentIndex;
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    [self getDataWithType:clickType];
}
#pragma mark ---更新  编辑  更多----
-(void)updateBtnClicked:(UIButton*)btn{
    NSLog(@"INDEX--%ld",btn.tag);
}
-(void)editBtnClicked:(UIButton*)btn{
    NSLog(@"EDIT-INDEX--%ld",btn.tag);
}
-(void)moreBtnClicked:(UIButton*)btn{
    NSLog(@"MORE-INDEX--%ld",btn.tag);
    clickIndex = btn.tag;
    [self alertWithTitle1:@"取消" title2:@"分享" title3:@"删除"];
}
#pragma mark ----更多操作---
-(void)actionSheetIndex0Function{
    NSLog(@"取消1");
}
-(void)actionSheetIndex1Function{
    NSLog(@"分享");
    NSDictionary * dic = self.xhArr[clickIndex-1001];
    NSString * title = dic[@"title"];
    [self shareWithContent:title Title:@"铁合金现货网App" Url:@"https://itunes.apple.com/cn/app/%E9%93%81%E5%90%88%E9%87%91%E7%8E%B0%E8%B4%A7%E7%BD%91/id1179909421?mt=8"];
}
-(void)actionSheetIndex2Function{
    [MBProgressHUD showMessage:@"正在删除" toView:self.view];
    NSDictionary * dic = self.xhArr[clickIndex - 1001];
    NSString * itemid = dic[@"itemid"];
    NSString * action;
    switch (clickType) {
        case 0:
            action = @"dxsqg";
            break;
        case 1:
            action = @"dxh";
            break;
        case 2:
            action = @"dgy";
            break;
        case 3:
            action = @"dqg";
            break;
        default:
            break;
    }
    //NSString * urlstr =[NSString stringWithFormat:@"%@%@%@%@%@%@",THJDomain,THJStorePHP,@"action=",action,@"&itemid=",itemid];
    NSString * urlstr =[NSString stringWithFormat:@"%@%@",THJDomain,THJStorePHP];
    __weak typeof(self) weakself = self;
    NSDictionary * paraDic = @{
                               @"action":action,
                               @"itemid":itemid
                               };
    
    [THJNetWork postNetDataFromUrl:urlstr parameters:paraDic callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        NSLog(@"receivedaga--%@",receiveData);
        if ([receiveData[@"state"] isEqual:@"success"]) {
            [weakself HUDShowMsg:@"删除成功" AfterDelayDimiss:1];
            [weakself getDataWithType:clickType];
        }else{
            [weakself HUDShowMsg:@"删除失败" AfterDelayDimiss:1];
        }
    }];
//    [THJNetWork getNetDataFromUrl:urlstr callback:^(NSDictionary* receiveData, NSError *error) {
//        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
//        NSLog(@"receivedaga--%@",receiveData);
//        if ([receiveData[@"state"] isEqual:@"success"]) {
//            [weakself HUDShowMsg:@"删除成功" AfterDelayDimiss:1];
//            [weakself getDataWithType:clickType];
//        }else{
//            [weakself HUDShowMsg:@"删除失败" AfterDelayDimiss:1];
//        }
//    }];
}
@end
